import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:alipush_flutter/alipush_flutter.dart';

void main() {
  runApp(HomPage());
}

class HomPage extends StatelessWidget {
  const HomPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:MyApp() ,
    );
  }
}


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final AlipushFlutter push = new AlipushFlutter();
  String pushTitle = '';
  String pushBody = '';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    push.addEventHandler(
        onReceiveNotification: (Map<String, dynamic> message) async {
          print("flutter onReceiveNotification: $message");
          String title = message["aps"]["alert"]["title"];
          String body = message["aps"]["alert"]["body"];
          pushTitle = title;
          pushBody = body;
          showPushAlert(title,body);
        },
        onOpenNotification: (Map<String, dynamic> message) async {
          String title = message["aps"]["alert"]["title"];
          String body = message["aps"]["alert"]["body"];
          pushTitle = title;
          pushBody = body;
          showPushAlert(title,body);
          print("flutter onOpenNotification: $message");
    });

    push.setup(
        appKey: "333495316",
        appSecret: "b2203250524d46cf85e18c9252979acc"
    );
    push.setLogEnable(true);
    if (!mounted) return;

    setState(() {});
  }

  void showPushAlert(String title, String body) {
    showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text(title),
          content: Text(body),
          actions: <Widget>[
            new ElevatedButton(
              child: new Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            new ElevatedButton(
              child: new Text("确定"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(onPressed: (){
                push.setAlias("alias").then((value){
                  print("别名设置是否成功$value");
                });
              }, child:Text("设置别名") ),
              ElevatedButton(onPressed: (){
                push.deleteAlias("alias").then((value){
                  print("别名删除是否成功$value");
                });
              }, child:Text("删除别名") ),
              ElevatedButton(onPressed: (){
                push.setBadge(88);}
                , child:Text("设置小红点") ),
              ElevatedButton(onPressed: (){
                showPushAlert(pushTitle, pushBody);}
                  , child:Text("展示上一条推送信息") )
            ],
          ),
        )
      );
  }
}
